package com.wfuhui.modules.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.wfuhui.modules.member.dao.MemberDao;
import com.wfuhui.modules.member.entity.MemberEntity;
import com.wfuhui.modules.store.dao.StoreDao;
import com.wfuhui.modules.store.entity.StoreEntity;
import com.wfuhui.modules.store.service.StoreService;
import org.springframework.transaction.annotation.Transactional;


@Service("storeService")
public class StoreServiceImpl implements StoreService {
	@Autowired
	private StoreDao storeDao;
	@Autowired
	private MemberDao memberDao;
	
	@Override
	public StoreEntity queryObject(Integer id){
		StoreEntity store = storeDao.queryObject(id);
		MemberEntity user = memberDao.queryObject(store.getUserId());
		store.setUser(user);
		return store;
	}
	
	@Override
	public List<StoreEntity> queryList(Map<String, Object> map){
		return storeDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return storeDao.queryTotal(map);
	}
	
	@Override
	public void save(StoreEntity store){
		MemberEntity user = new MemberEntity();
		user.setRealName(store.getRealName());
		user.setMobile(store.getMobile());
		user.setId(store.getUserId());
		memberDao.update(user);
		storeDao.save(store);
	}
	
	@Override
	public void update(StoreEntity store){
		storeDao.update(store);
	}
	
	@Override
	public void delete(Integer id){
		storeDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		storeDao.deleteBatch(ids);
	}

	@Override
	public List<StoreEntity> queryNearby(Map<String, Object> params) {
		return storeDao.queryNearby(params);
	}

	@Override
	@Transactional
	public void examine(StoreEntity store) {
		storeDao.update(store);
		store = storeDao.queryObject(store.getId());
		if(store.getStatus() == 2){
			MemberEntity user = new MemberEntity();
			user.setId(store.getUserId());
			user.setUserType(2);
			user.setStoreId(store.getId());
			memberDao.update(user);
		}else if(store.getStatus() == 3){
			MemberEntity user = new MemberEntity();
			user.setId(store.getUserId());
			user.setUserType(1);
			memberDao.update(user);
		}
	}

	@Override
	public StoreEntity queryByUserId(Long userId) {
		return storeDao.queryByUserId(userId);
	}

}
